package com.qf.test;

import com.qf.pojo.User;
import org.junit.Test;
import org.springframework.util.SerializationUtils;
import redis.clients.jedis.Jedis;

import java.util.Date;

//测试byte数组
public class Demo2 {
    @Test
    public void testSet(){
        //1 连接redis
        Jedis jedis = new Jedis("192.168.200.129", 6379);
        //2 准备key和value值, 都要是byte数组类型
        User user = new User(1, "飞影铠甲", new Date());
        byte[] key = SerializationUtils.serialize("user");
        byte[] value = SerializationUtils.serialize(user);
        //3 使用jedis存入byte[]类型到redis数据
        jedis.set(key,value);


        //4 关闭资源
        jedis.close();

    }
    //从redis中获取byte[]格式的数据
    @Test
    public void testGet(){
        //1 连接redis
        Jedis jedis = new Jedis("192.168.200.129", 6379);
        //2.准备byte[]类型的key
        byte[] key = SerializationUtils.serialize("user");
        //3.根据key到redis中获取数据，获取到的也是byte[]类型数据
        byte[] value = jedis.get(key);
        //4将获取到的value值进行反序列化
        User user = (User) SerializationUtils.deserialize(value);
        System.out.println("---------" + user);
        //5 释放资源
        jedis.close();





    }
}
